package com.shengde.jinwawaforleader.model.myJob.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseV4Fragment;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;
import com.shengde.jinwawaforleader.model.myJob.classList.BindingIdCardActivity;
import com.shengde.jinwawaforleader.model.myJob.classList.ChangeTeacherActivity;
import com.shengde.jinwawaforleader.model.myJob.classList.ClassListActivity;
import com.shengde.jinwawaforleader.model.myJob.classList.WorkOfTeacherActivity;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProClassList;
import com.shengde.jinwawaforleader.views.ModifyTeaStuPopupWindow;

/**
 * Created by Chason on 2016/9/8.
 */

public class AllTeacherFragment extends BaseV4Fragment {
    private static final String TAG = "AllTeacherFragment";
    private LinearLayout view;
    private ListView lv;
    private ClassListActivity activity;
    private CommonBaseAdapter<ProClassList.TeacherEntity> adapter;
    private ModifyTeaStuPopupWindow showPopupWindow;
    private String teacherName;
    private int teacherId;
    private int shujuweizhi;

    @Override
    public View initView(LayoutInflater inflater) {
        view = (LinearLayout) inflater.inflate(R.layout.fragment_all_teacher, null);
        return view;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        activity = (ClassListActivity) getActivity();
        lv = (ListView) view.findViewById(R.id.lv);
    }

    @Override
    public void listener() {

    }

    @Override
    public void onWidgetClick(View view) {

    }

    public void paddingData() {
        if (activity.teacher != null) {
            if (adapter == null) {
                adapter = new CommonBaseAdapter<ProClassList.TeacherEntity>(activity, activity.teacher, R.layout.all_teacher_item) {
                    @Override
                    public void convert(ViewHolder holder, final int position) {
                        ImageView touxiang = holder.getView(R.id.touxiang);
                        TextView name = holder.getView(R.id.name);
                        TextView number = holder.getView(R.id.number);
                        TextView class_name = holder.getView(R.id.class_name);
                        TextView change = holder.getView(R.id.change);
                        Glide.with(activity).load(BaseProtocol.IMG_BASE + activity.teacher.get(position).getLogo()).error(R.drawable.touxiang).into(touxiang);
                        name.setText(activity.teacher.get(position).getName());
                        number.setText(activity.teacher.get(position).getPhone());
                        class_name.setText(activity.teacher.get(position).getNum() + "个");

                        touxiang.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(activity, WorkOfTeacherActivity.class);
                                intent.putExtra("teacherid", activity.teacher.get(position).getUserid() + "");
                                intent.putExtra("teachername", activity.teacher.get(position).getName());
                                Log.i(TAG, "onClick: teacherid " + activity.teacher.get(position).getUserid());
                                startActivity(intent);
                            }

                        });
                        number.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                TextView tv_num = (TextView) v;
                                Intent intent = new Intent(Intent.ACTION_DIAL);
                                intent.setData(Uri.parse("tel:" + tv_num.getText().toString()));
                                activity.startActivity(intent);
                            }
                        });
                        final String t_name = name.getText().toString();
                        final int teacherid = activity.teacher.get(position).getTeacherid();
                        final int ls_shujuweizhi = position;
                        change.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                showPopupWindow(v, t_name, teacherid, ls_shujuweizhi);
                            }
                        });
                    }
                };
                lv.setAdapter(adapter);
            } else {
                adapter.resetList(activity.teacher);
            }
        }

    }

    //显示 弹窗
    private void showPopupWindow(View v, String name, int id, int weizhi) {
        teacherName = name;
        teacherId = id;
        shujuweizhi = weizhi;
        showPopupWindow = new ModifyTeaStuPopupWindow(activity, itemsOnClick);
        showPopupWindow.tv_video_babydiary_share.setText("更换");
        showPopupWindow.tv_video_babydiary_delete.setText("删除");

        //设置layout在PopupWindow中显示的位置
        showPopupWindow.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    //为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {

        public void onClick(View v) {
            showPopupWindow.dismiss();
            switch (v.getId()) {
                case R.id.tv_video_babydiary_share:
                    Intent intent = new Intent(activity, ChangeTeacherActivity.class);
                    intent.putExtra("name", teacherName);
                    intent.putExtra("teacherid", teacherId);
                    startActivity(intent);
                    showPopupWindow.dismiss();
                    break;
                case R.id.tv_video_babydiary_delete:
                    BaseProtocol.getInstance().deleteTeacherInfo(getActivity(), teacherId + "");
                    showPopupWindow.dismiss();
                    break;
                case R.id.tv_video_babydiary_cancle:
                    showPopupWindow.dismiss();
                    break;
                case R.id.tv_modify:
                    BaseProtocol.getInstance().getTeacherInfo(activity, teacherId + "");
                    showPopupWindow.dismiss();
                    break;
                case R.id.tv_binding_id_card:
                    intent = new Intent(acticity, BindingIdCardActivity.class);
                    intent.putExtra("teacherid", teacherId + "");
                    intent.putExtra("type", "1");
                    startActivity(intent);
                    break;
            }
        }
    };
}